package netTest;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

public class URLDemo {
    public static void main(String[] args) throws  Exception{
        int c;
        URL hp=new URL("http://portal.uestc.edu.cn/authserver/custom/js/login.js");
        URLConnection hpCon=hp.openConnection();

        System.out.println("Protocol: "+hp.getProtocol());
        System.out.println("Port: "+hp.getPort());

        System.out.println("Host: "+hp.getHost());
        System.out.println("File: "+hp.getFile());
        System.out.println("Ext:"+hp.toExternalForm());


        long d=hpCon.getDate();
        if (d==0){
            System.out.println("No date information.");
        }else {
            System.out.println("Date: "+ new Date(d));
        }

        //get content type
        d=hpCon.getExpiration();
        if (d==0){
            System.out.println("No expiration information.");
        }else {
            System.out.println("Expires: "+ new Date(d));
        }

        //get content lenght
        long len=hpCon.getContentLength();
        if (len==-1){
            System.out.println("Content length unavaiable.");
        }else {
            System.out.println("Content-Length: "+len);
        }

        if (len!=0){
            System.out.println("===Content===");
            InputStream in= hpCon.getInputStream();
            while ((c=in.read())!=-1){
                System.out.print((char)c);
            }
            in.close();
        }else {
            System.out.println("No content avaiable.");
        }

    }
}
